import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-11-02
 * Time: 16:26
 */
public class Demo4 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000);
        Thread producer = new Thread(() -> {
            int a = 0;
            while(true) {
                try {
                    queue.put(a);
                    System.out.println("生产元素" + a);
                    a++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread consumer = new Thread(() -> {
            while(true) {
                try {
                    Integer a = queue.take();
                    System.out.println("消费元素" + a);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        producer.start();
        consumer.start();
    }
}
